#include <iostream>
#include "miniutil/supnpmanager.h"
#include "upnpcommands.h"
#include "miniutil/thread.h"
#include "minitest/testcase.h"

class testupnp: public testcase
{
public:
	testupnp()
	{
	}

	virtual void test()
	{
#ifdef WIN32
		{WSADATA data; WSAStartup(MAKEWORD(2,2), &data);}
#endif
		//getAvailableMapping();
		testsimpleManager();
	}

	void testsimpleManager()
	{
		miniutil::upnp::simpleManager* pmgr = 
			new miniutil::upnp::simpleManager("192.168.1.173", 1111, 1333, -1, -1);
		if(pmgr->result())
			std::cout<<"upnp OK, external port:"<<pmgr->getExternalTcpPort()<<std::endl;
		else
			std::cout<<"upnp fail"<<std::endl;

		miniutil::thread::sleep(1000);
		delete pmgr;

	}

	void getAvailableMapping()
	{
		char* controlURL = "http://192.168.1.1:80/upnp/control3";
		char* servicetype = "urn:schemas-upnp-org:service:WANIPConnection:1";//"urn:schemas-upnp-org:device:InternetGatewayDevice:1";
		char index[10];
		char extPort[32], intClient[128], intPort[32], protocol[512],
			 desc[512], enabled[32], rHost[128], duration[64];

		memset(index, 0x00, sizeof(index));
		memset(extPort, 0x00, sizeof(extPort));
		memset(intClient, 0x00, sizeof(extPort));
		memset(intPort, 0x00, sizeof(extPort));
		memset(desc, 0x00, sizeof(extPort));
		memset(enabled, 0x00, sizeof(extPort));
		memset(rHost, 0x00, sizeof(extPort));
		memset(duration, 0x00, sizeof(extPort));

		int i=0;
		while(true)
		{
			sprintf((char*)index, "%d", i);
			int ret = miniutil::upnp::UPNP_GetGenericPortMappingEntry(controlURL,servicetype,(char*)index,
				extPort, intClient, intPort, protocol, desc, enabled, rHost, duration);
			if(ret == UPNPCOMMAND_SUCCESS)
			{
				std::cout<<extPort<<"||"<<intClient<<"||"<<protocol<<"||"<<desc<<"||"<<enabled
					<<"||"<<rHost<<"||"<<duration<<std::endl;
			}
			else
			{
				std::cout<<index<<" operation Fail"<<std::endl;
				break;
			}
			i++;
		}
	}

};

testcase * testupnp1 = new testupnp();
